package com.dongdong.service.Impl;

import com.dongdong.mapper.TbAddressMapper;
import com.dongdong.model.TbAddress;
import com.dongdong.model.TbAddressExample;
import com.dongdong.service.AddressService;
import com.dongdong.vo.AddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private TbAddressMapper tbAddressMapper;;

    @Override
    public List<AddressVO> selectAddressByUsername(String username) {
        TbAddressExample example = new TbAddressExample();
        example.createCriteria().andUserIdEqualTo(username);
        List<TbAddress> tbAddresses = tbAddressMapper.selectByExample(example);

      //转换
        List<AddressVO> vos = tbAddresses.stream().map((x) ->{
            AddressVO vo = new AddressVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return vos;
    }

    @Override
    public TbAddress selectAddrById(Long id) {
        return tbAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insert(AddressVO addressVO) {
        TbAddress tbAddress=new TbAddress();
        BeanUtils.copyProperties(addressVO, tbAddress);

        return tbAddressMapper.insertSelective(tbAddress);
    }


}
